//
//  ToastView.m
//  youtx
//
//  Created by Zan Yunfei on 13-9-5.
//
//

#import "ToastView.h"
#import <QuartzCore/QuartzCore.h>



@implementation ToastView
@synthesize toastView;
@synthesize callblock;
- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    return self;
}

-(void)dealloc
{
    [toastView release];
    [super dealloc];
}
//+ (CallbackBlock)callback{
//    CallbackBlock block = ^(NSString *str){
//        NSLog(@"234234234234");
//    };
//    return block;
//}
+ (void)showToastAndActivitViewWithContent:(NSString *)content andRect:(CGRect)rect andTime:(float)time andObject:(UIViewController *)selfController CallbackBlock:(CallbackBlock)resultBlock{
    
    if ([selfController.view viewWithTag:1234554321]) {
        UIView * tView = [selfController.view viewWithTag:1234554321];
        [tView removeFromSuperview];
    }
    
    UIImageView *toastView = [[UIImageView alloc] initWithFrame:rect];
    
    if (IOS7) {
        if ([UIImage imageNamed:@"ToastView_bg.png"]) {
            [toastView setImage:[[UIImage imageNamed:@"ToastView_bg.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
        }else{
            [toastView setBackgroundColor:[UIColor blackColor]];
        }
    }else{
        if ([UIImage imageNamed:@"toastBackImage.png"]) {
            [toastView setImage:[[UIImage imageNamed:@"toastBackImage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
        }else{
            [toastView setBackgroundColor:[UIColor blackColor]];
        }
    }
    [toastView.layer setCornerRadius:5.0f];
    [toastView.layer setMasksToBounds:YES];
    [toastView setAlpha:1.0f];
    [toastView setTag:1234554321];
    [selfController.view addSubview:toastView];
    [toastView release];
    
    
    
//    CGSize labelSize = [content sizeWithFont:[UIFont systemFontOfSize:17.0f]
//                           constrainedToSize: CGSizeMake( rect.size.width ,MAXFLOAT)
//                               lineBreakMode: NSLineBreakByWordWrapping];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17.0f], NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize = [content boundingRectWithSize:CGSizeMake(rect.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    if (labelSize.height > rect.size.height) {
        [toastView setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, labelSize.height)];
    }
//    CGSize labelSize1 = [content sizeWithFont:[UIFont systemFontOfSize:14.0f]
//                           constrainedToSize: CGSizeMake( rect.size.width ,MAXFLOAT)
//                               lineBreakMode: UILineBreakModeWordWrap];
    NSDictionary *attribute1 = @{NSFontAttributeName:[UIFont systemFontOfSize:14.0f], NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize1 = [content boundingRectWithSize:CGSizeMake(rect.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute1 context:nil].size;
    
    UILabel * contentLabel = [[UILabel alloc] initWithFrame:CGRectMake((toastView.frame.size.width-labelSize1.width)/2+6, (toastView.frame.size.height-labelSize1.height)/2, labelSize1.width, labelSize1.height)];
    [contentLabel setText:content];
    [contentLabel setTextColor:[UIColor whiteColor]];
    [contentLabel setFont:[UIFont systemFontOfSize:14.0f]];
    [contentLabel setTextAlignment:NSTextAlignmentCenter];
    [contentLabel setBackgroundColor:[UIColor clearColor]];
    [contentLabel setNumberOfLines:0];
    [contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [toastView addSubview:contentLabel];
    [contentLabel release];
    
    UIActivityIndicatorView *tempactivi = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((toastView.frame.size.width-labelSize1.width)/2-14,(toastView.frame.size.height-15)/2, 15, 15)];
    tempactivi.tag = (long)201405191842;
    [toastView addSubview:tempactivi];
    [tempactivi startAnimating];
    [tempactivi release];
    if (time>0.01) {
        //NSArray *tempArray = @[selfController,resultBlock];
        [self performSelector:@selector(removeToastView:) withObject:selfController afterDelay:time];
        //sleep(1);
        double delayInSeconds = 2.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
           return resultBlock(@"ok");
        });
    }
}

+ (void)showToastViewWithContent:(NSString *)content andRect:(CGRect)rect andTime:(float)time andObject:(UIViewController *)selfController
{
    if ([selfController.view viewWithTag:1234554321]) {
        UIView * tView = [selfController.view viewWithTag:1234554321];
        [tView removeFromSuperview];
    }
    
    UIImageView * toastView = [[UIImageView alloc] initWithFrame:rect];
    
    if (IOS7) {
        if ([UIImage imageNamed:@"ToastView_bg.png"]) {
            [toastView setImage:[[UIImage imageNamed:@"ToastView_bg.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
        }else{
            [toastView setBackgroundColor:[UIColor blackColor]];
        }
    }else{
        if ([UIImage imageNamed:@"toastBackImage.png"]) {
            [toastView setImage:[[UIImage imageNamed:@"toastBackImage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
        }else{
            [toastView setBackgroundColor:[UIColor blackColor]];
        }
    }
    [toastView.layer setCornerRadius:5.0f];
    [toastView.layer setMasksToBounds:YES];
    [toastView setAlpha:1.0f];
    [toastView setTag:1234554321];
    [selfController.view addSubview:toastView];
    [toastView release];
    
//    CGSize labelSize = [content sizeWithFont:[UIFont systemFontOfSize:17.0f]
//                           constrainedToSize: CGSizeMake( rect.size.width ,MAXFLOAT)
//                               lineBreakMode: UILineBreakModeWordWrap];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17.0f], NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize = [content boundingRectWithSize:CGSizeMake(rect.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    //UILabel * contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, rect.size.width-20, labelSize.height)];
    //UILabel * contentLabel = [[UILabel alloc] initWithFrame:toastView.bounds];
    if (labelSize.height > rect.size.height) {
        [toastView setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, labelSize.height)];
    }
    UILabel * contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, toastView.frame.size.width - 20, toastView.frame.size.height)];
    [contentLabel setText:content];
    [contentLabel setTextColor:[UIColor whiteColor]];
    [contentLabel setFont:[UIFont systemFontOfSize:14.0f]];
    [contentLabel setTextAlignment:NSTextAlignmentCenter];
    [contentLabel setBackgroundColor:[UIColor clearColor]];
    [contentLabel setNumberOfLines:0];
    [contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [toastView addSubview:contentLabel];
    [contentLabel release];
    
    if (time>0.01) {
        [self performSelector:@selector(removeToastView:) withObject:selfController afterDelay:time];
    }
    
}

+ (void)showToastViewWithContent:(NSString *)content andRect:(CGRect)rect andTime:(float)time andObject:(UIViewController *)selfController andHightNoLimited:(BOOL)limited
{
    if ([selfController.view viewWithTag:1234554321]) {
        UIView * tView = [selfController.view viewWithTag:1234554321];
        [tView removeFromSuperview];
    }
    
    UIImageView * toastView = [[UIImageView alloc] initWithFrame:rect];
    //[toastView setBackgroundColor:[UIColor blackColor]];
    if (IOS7) {
        if ([UIImage imageNamed:@"ToastView_bg.png"]) {
            [toastView setImage:[[UIImage imageNamed:@"ToastView_bg.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
        }else{
            [toastView setBackgroundColor:[UIColor blackColor]];
        }
    }else{
        if ([UIImage imageNamed:@"toastBackImage.png"]) {
            [toastView setImage:[[UIImage imageNamed:@"toastBackImage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
        }else{
            [toastView setBackgroundColor:[UIColor blackColor]];
        }
    }

    [toastView.layer setCornerRadius:5.0f];
    [toastView.layer setMasksToBounds:YES];
    [toastView setAlpha:1.0f];
    [toastView setTag:1234554321];
    [selfController.view addSubview:toastView];
    [toastView release];
    
    UILabel * contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 200, 40)];
	
    [contentLabel setText:content];
    [contentLabel setTextColor:[UIColor whiteColor]];
    [contentLabel setTextAlignment:NSTextAlignmentCenter];
    [contentLabel setBackgroundColor:[UIColor clearColor]];
    [contentLabel setNumberOfLines:0];
    [toastView addSubview:contentLabel];
    [contentLabel release];
    
    [self performSelector:@selector(removeToastView:) withObject:selfController afterDelay:time];
}
+ (void)showToastAndActivitViewWithContent:(NSString *)content andRect:(CGRect)rect andObject:(UIViewController *)selfController{
    if ([selfController.view viewWithTag:1234554321]) {
        UIView * tView = [selfController.view viewWithTag:1234554321];
        [tView removeFromSuperview];
    }
    
    UIImageView *toastView = [[UIImageView alloc] initWithFrame:rect];
    
    if (IOS7) {
        if ([UIImage imageNamed:@"ToastView_bg.png"]) {
            [toastView setImage:[[UIImage imageNamed:@"ToastView_bg.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
        }else{
            [toastView setBackgroundColor:[UIColor blackColor]];
        }
    }else{
        if ([UIImage imageNamed:@"toastBackImage.png"]) {
            [toastView setImage:[[UIImage imageNamed:@"toastBackImage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
        }else{
            [toastView setBackgroundColor:[UIColor blackColor]];
        }
    }
    [toastView.layer setCornerRadius:5.0f];
    [toastView.layer setMasksToBounds:YES];
    [toastView setAlpha:1.0f];
    [toastView setTag:1234554321];
    [selfController.view addSubview:toastView];
    [toastView release];
    
//    CGSize labelSize = [content sizeWithFont:[UIFont systemFontOfSize:17.0f]
//                           constrainedToSize: CGSizeMake( rect.size.width ,MAXFLOAT)
//                               lineBreakMode: UILineBreakModeWordWrap];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17.0f], NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize = [content boundingRectWithSize:CGSizeMake(rect.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    if (labelSize.height > rect.size.height) {
        [toastView setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, labelSize.height)];
    }
//    CGSize labelSize1 = [content sizeWithFont:[UIFont systemFontOfSize:14.0f]
//                            constrainedToSize: CGSizeMake( rect.size.width ,MAXFLOAT)
//                                lineBreakMode: UILineBreakModeWordWrap];
    NSDictionary *attribute1 = @{NSFontAttributeName:[UIFont systemFontOfSize:14.0f], NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize1 = [content boundingRectWithSize:CGSizeMake(rect.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute1 context:nil].size;
    
    UILabel * contentLabel = [[UILabel alloc] initWithFrame:CGRectMake((toastView.frame.size.width-labelSize1.width)/2+6, (toastView.frame.size.height-labelSize1.height)/2, labelSize1.width, labelSize1.height)];
    [contentLabel setText:content];
    [contentLabel setTextColor:[UIColor whiteColor]];
    [contentLabel setFont:[UIFont systemFontOfSize:14.0f]];
    [contentLabel setTextAlignment:NSTextAlignmentCenter];
    [contentLabel setBackgroundColor:[UIColor clearColor]];
    [contentLabel setNumberOfLines:0];
    [contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [toastView addSubview:contentLabel];
    [contentLabel release];
    
    UIActivityIndicatorView *tempactivi = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((toastView.frame.size.width-labelSize1.width)/2-14,(toastView.frame.size.height-15)/2, 15, 15)];
    tempactivi.tag = (long)201405191842;
    [toastView addSubview:tempactivi];
    [tempactivi startAnimating];
    [tempactivi release];
}

+ (void)removeToastView:(id)sender{
    //NSArray *array = (NSArray *)sender;
    //UIViewController * selfController = (UIViewController *)array[0];
    UIViewController * selfController = (UIViewController *)sender;
    UIView * toastView = [selfController.view viewWithTag:1234554321];
    [toastView removeFromSuperview];
    if ([selfController.view viewWithTag:(long)201405191842]) {
        UIActivityIndicatorView *TEMP = (UIActivityIndicatorView *)[selfController.view viewWithTag:(long)201405191842];
        [TEMP stopAnimating];
    }
    toastView = nil;
//    CallbackBlock block = (CallbackBlock)array[1];
//    block(@"ok123");
}

@end
